﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;

namespace Shine.Util.Serialization
{
    /// <summary>
    /// 数据序列化
    /// </summary>
    public class DataContractSerialization
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] Serialize(object obj)
        {
            return Serialize(obj, null);
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="knownTypes"></param>
        /// <returns></returns>
        public static byte[] Serialize(object obj, IEnumerable<Type> knownTypes)
        {
            if (obj == null)
                return null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                new DataContractSerializer(obj.GetType(), knownTypes).WriteObject(memoryStream, obj);
                return memoryStream.ToArray();
            }
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static object Deserialize<T>(byte[] bytes)
        {
            return Deserialize<T>(bytes, null);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <param name="knownTypes"></param>
        /// <returns></returns>
        public static object Deserialize<T>(byte[] bytes, IEnumerable<Type> knownTypes)
        {
            if (bytes == null)
                return null;
            using (MemoryStream menoryStream = new MemoryStream(bytes))
            {
                return new DataContractSerializer(typeof(T), knownTypes).ReadObject(menoryStream);
            }
        }

        /// <summary>
        /// 对象转换成json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonObject">需要格式化的对象</param>
        /// <returns>Json字符串</returns>
        public static string ToJson<T>(T jsonObject)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            string json = null;
            using (var ms = new MemoryStream()) //定义一个stream用来存发序列化之后的内容
            {
                serializer.WriteObject(ms, jsonObject);
                var dataBytes = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(dataBytes, 0, (int)ms.Length);
                json = Encoding.UTF8.GetString(dataBytes);
                ms.Close();
            }
            return json;
        }

        /// <summary>
        /// json字符串转换成对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json">要转换成对象的json字符串</param>
        /// <returns></returns>
        public static T FromJson<T>(string json)
        {
            var serializer = new DataContractJsonSerializer(typeof(T));
            var obj = default(T);
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                obj = (T)serializer.ReadObject(ms);
                ms.Close();
            }
            return obj;
        }
    }
}
